#Last-modified: 3:21:20 PM Friday, August 14, 2020

####################### Description ###############################################################################################################
#  
#  Covid-19 Project Data processing & Figure ploting
#  
#  @version: 1.0
#  @author:  Yanjun WEI
#  @contact: jason.yj.wei@gmail.com, ywei4@mdanderson.org
#
###################################################################################################################################################

# [STEP I] ==================================================================================================
# Related to Extended Data Table 1 & 2

### MAGeCK Analysis (MAGeCK v0.5.9.4)
### Perl (v5.32.0)
### python (v3.8.5)
# fastq files can be downloaded from https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE209750
# library can be downloaded from https://www.addgene.org/pooled-library/liu-crispr-knockout/
mageck count -l MAGeCK-library/human_sgrna_sequences_A-library_MAGeCK.txt --control-sgrna MAGeCK-library/human_sgrna_sequences_A-CTRL_MAGeCK.txt --norm-method control -n COVID-19_CRISPR_210212 --sample-label C-1,C-2,C-3,control1,control2,control3,Ref-1 --fastq C-1L1.fastq,C-1L2.fastq,C-1L3.fastq,C-1L4.fastq C-2L1.fastq,C-2L2.fastq,C-2L3.fastq,C-2L4.fastq C-3L1.fastq,C-3L2.fastq,C-3L3.fastq,C-3L4.fastq control1L1.fastq,control1L2.fastq,control1L3.fastq,control1L4.fastq control2L1.fastq,control2L2.fastq,control2L3.fastq,control2L4.fastq control3L1.fastq,control3L2.fastq,control3L3.fastq,control3L4.fastq Ref-1L1.fastq,Ref-1L2.fastq,Ref-1L3.fastq,Ref-1L4.fastq
mageck test -k COVID-19_CRISPR_210212.count.txt -t C-1,C-2,C-3 -c control1,control2,control3 --control-sgrna MAGeCK-library/human_sgrna_sequences_A-CTRL_MAGeCK.txt --norm-method control --keep-tmp -n COVID-19_CRISPR_210212_C_control --gene-lfc-method secondbest


# [STEP II] =================================================================================================
# Related to Extended Data Figure 2a & 2c

### R (v4.1.3)
# sgRNA count table can be downloaded from https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE209750
CountTab = "COVID-19_CRISPR_210212.count.txt"

InputData <- read.delim(CountTab, row.names=1, check.names = FALSE)
head(InputData)

OutputPrefix = "COVID-19_CRISPR_210212"

library <- read.delim("human_sgrna_sequences_A-library_MAGeCK.txt", header=FALSE, row.names=1)
head(library)
## Prepare
SamplesNum = 7
fromWhichColumn = 2
InputData_counts = matrix(0, dim(InputData)[1], SamplesNum)
rownames(InputData_counts) <- rownames(library)
colnames(InputData_counts) <- colnames(InputData)[fromWhichColumn:dim(InputData)[2]]
for (sg in rownames(InputData_counts)) {
  for (sample in colnames(InputData_counts)) {
    InputData_counts[sg, sample] = InputData[sg, sample]
  }
}
head(InputData_counts)

#-EssentialGene PLOT-----------------------------------------------------------------------------------#
# The Core fitness genes (n=1580) download from http://tko.ccbr.utoronto.ca/, "EssentialGeneList.human.Ortholog.tab===mouse.xlsx" (can be downloaded from https://github.com/Proteome-EPI-Genome/Screen-public/tree/main/EssentialGenes) is an Ortholog annotated version based on our in-house script.
library(readxl)
EssentialGeneList_human_Ortholog_tab_mouse <- read_excel("EssentialGeneList.human.Ortholog.tab===mouse.xlsx")
head(EssentialGeneList_human_Ortholog_tab_mouse)

InputData$essential = rep(0,dim(InputData)[1])
lineNum = 0
for (MEG in EssentialGeneList_human_Ortholog_tab_mouse$hs_geneId){
  lineNum = lineNum + 1
  if (MEG == "-"){
    #print(EssentialGeneList_human_Ortholog_tab_mouse[lineNum,])
  }else{
    if (grepl(";", MEG, fixed=TRUE)){
      #print(MEG)
      MEGs = unlist(strsplit(MEG, ";"))
      for (subMEG in MEGs){
        #print(subMEG)
        InputData[which(InputData$Gene == subMEG), "essential"] = 1
      }
    }else{
      InputData[which(InputData$Gene == MEG), "essential"] = 1
    }
  }
}

## ESSENTIAL GENE PLOT
library(sm)

pdf(paste(OutputPrefix, "_Essential&NonEssential_DensityPlot.pdf", sep = ""), width=11, height=8.5)
par(mfrow=c(4,3), mar=c(5,5,2,2)+0.1, mgp=c(3,1,0))
for (S in 2:dim(InputData)[2]){
  y <- as.vector(InputData[, S])
  y <- log2(y+1)
  g <- InputData$essential
  
  colfill <- c("#1C70AD","#F77B10")
  
  # create value labels
  g.f <- factor(g, levels= 0:1, labels = c("Non-essential", "Essential"))
  
  # plot densities
  sm.density.compare(y, g, xlab="log2(readcount)", col=colfill, lwd = 3, lty = rep(1,3), xlim = c(-3, 20), ylim = c(0, 0.5))
  title(main=colnames(InputData)[S])
  legend("topright", levels(g.f), fill=colfill, bty = "n")
}
dev.off()


# [STEP III] ================================================================================================
# Related to Extended Data Figure 2b

## Correlation
library(corrplot)

M_pearson <- cor(log2(InputData_counts + 1), method = "pearson")
pdf(paste(OutputPrefix, "_Cor(pearson).pdf", sep = ""), width=11, height=8.5)
corrplot.mixed(M_pearson)
text(0, 5, SampleType, srt = 90, cex = 2)
dev.off()


# [STEP IIII] ===============================================================================================
# Related to Figure 1b

## ScatterPlot
library(ggpubr)
# Screen result table can be downloaded from https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE209750
Data<- read.delim("COVID-19_CRISPR_210212.MAGeCK_secondbest.CTRL.genelevel.merged.txt")

Data$C.Ctrl_neg.p.value = -log10(Data$C.Ctrl_neg.p.value)
Data$C.Ctrl_pos.p.value = -log10(Data$C.Ctrl_pos.p.value)

Data$enrich = ifelse(Data$C.Ctrl_pos.lfc >= 0.5 & Data$C.Ctrl_pos.p.value > -log10(0.05), 
                     'enrich',
                     'Stable')
Data$depletion = ifelse(Data$C.Ctrl_neg.lfc <= -0.5 & Data$C.Ctrl_neg.p.value > -log10(0.05), 
                        'depletion',
                        'Stable')
						

Data$C.Ctrl_neg.score = -log10(Data$C.Ctrl_neg.score)
Data$C.Ctrl_pos.score = -log10(Data$C.Ctrl_pos.score)

pdf(paste("COVID-19_CRISPR_210212", "C.Ctrl", "ScatterPlot.2022.05.13-score.pdf", sep = "."), width=11.5, height=8.5)
# enrich
enrichPlot <- ggscatter(Data, 
                        x = "C.Ctrl_pos.lfc", 
                        y = "C.Ctrl_pos.score", 
                        ylab = "-log10 (MAGeCK Score)",
                        xlab = "logFC",
                        xlim = c(0, 2),
                        #ylim = c(0, 7),
                        color = "enrich",
                        show.legend = FALSE,
                        size = ifelse(Data$C.Ctrl_pos.rank <= 30 & Data$enrich == "enrich", 2, 1),
                        label = "Gene", 
                        repel = T,
                        palette = c("red", "#999999") ,
                        label.select = Data$Gene[which(Data$C.Ctrl_pos.rank <= 30 & Data$enrich == "enrich")],
                        font.label = c(14, "bold")
)
# depletion
depletionPlot <- ggscatter(Data,
                           x = "C.Ctrl_neg.lfc",
                           y = "C.Ctrl_neg.score",
                           ylab = "-log10 (MAGeCK Score)",
                           xlab = "logFC",
                           xlim = c(-2, 0),
                           #ylim = c(0, 7),
                           color = "depletion",
                           show.legend = FALSE,
                           size = ifelse(Data$C.Ctrl_neg.rank <= 30 & Data$depletion == "depletion", 2, 1),
                           label = "Gene", 
                           repel = T,
                           palette = c("blue", "#999999") ,
                           label.select = Data$Gene[which(Data$C.Ctrl_neg.rank <= 30 & Data$depletion == "depletion")],
                           font.label = c(14, "bold")
)
ggarrange(depletionPlot, enrichPlot, ncol = 2, nrow = 1)
dev.off()


# [STEP IV] =================================================================================================
# Related to Figure 2a & 2b

Cytoscape (v3.8.2)
BioGRID database (Release 4.4.205)


# [STEP V] ==================================================================================================
# Related to Figure 2c

The COVID-19 GWAS meta-analyses results (release 6)
PhenoGram.RitchieLab (v1.2.1) http://visualization.ritchielab.org/phenograms/plot


# [STEP VI] =================================================================================================
Adobe Illustrator 2022